package com.example.Sao.MyActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.TabLayout;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.AppCompatImageView;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Toast;

import com.bin.david.form.annotation.SmartTable;
import com.bin.david.form.data.CellInfo;
import com.bin.david.form.data.column.Column;
import com.bin.david.form.data.format.bg.BaseBackgroundFormat;
import com.bin.david.form.data.format.bg.BaseCellBackgroundFormat;
import com.bin.david.form.data.style.FontStyle;
import com.example.Sao.Adapter.MyFragmentPagerAdapter;
import com.example.Sao.Custom.MyViewPager;
import com.example.Sao.Custom.RxTimer;
import com.example.Sao.Data.LogInfo;
import com.example.Sao.MyFragment.CensusFragment;
import com.example.Sao.ObjectBox.DeviceEntity;
import com.example.Sao.ObjectBox.DeviceEntity_;
import com.example.Sao.R;
import com.example.Sao.Tools.ObjectBoxHelper;
import com.example.Sao.Tools.SharedPrefsUtil;
import com.example.Sao.Tools.StatusColorUtil;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.disposables.Disposable;

public class LogActivity extends  MyBaseActivity implements View.OnClickListener{
    private int choose = 0;
    //单选框
    private AlertDialog alertDialog2;
    //数据库使用帮助者
    private ObjectBoxHelper objectBoxHelper;
    //日志tag
    private String TAG ="MainActivity";
    //控件
    private com.bin.david.form.core.SmartTable<LogInfo> table;
    private FloatingActionButton iv_back;
//    private TabLayout mTabLayout;
//    private MyViewPager mViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        //设置布局
        setContentView(R.layout.activity_census);
        //设置状态栏颜色
        StatusColorUtil.setColor(LogActivity.this,getResources().getColor(R.color.colorPrimary));
        //绑定控件
        initview();
        //初始数据库使用对象
        objectBoxHelper = ObjectBoxHelper.getInstance();
    }

    @Override
    protected void onResume() {
        super.onResume();
        //初始化日志列表数据
        initable();
        //初始化viewpager
//        initviewpager();
    }

    /**
     * 监听home键
     */
    @Override
    public void onBackPressed() {
        super.onBackPressed();
        startActivity(new Intent(LogActivity.this,MainActivity.class));
        this.finish();
    }

    /**
     * 绑定控件
     */
    private void initview(){
        table = (com.bin.david.form.core.SmartTable<LogInfo>) findViewById(R.id.table);
        iv_back = findViewById(R.id.floatbutton);
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LogActivity.this.finish();
            }
        });
//        mViewPager  =findViewById(R.id.mViewPager);
//        mTabLayout = findViewById(R.id.mTabLayout);
    }

    /**
     * 初始化列表
     */
    private void initable(){
        List<LogInfo>infoList = new ArrayList<>();
        //查询库里全部数据
       List<DeviceEntity>entities =  objectBoxHelper.Query("user", DeviceEntity_.username);
       for (DeviceEntity entity:entities){
           //Time为空对象说明该设备还未盘点
           if (entity.getTime()!=null&&!"".equals(entity.getTime())&&entity.isStaus()==true) {
               LogInfo info = new LogInfo(entity.getTime(), entity.getDeviceStreamCode(), entity.getDeviceName(), entity.getUseDepartment(),
                       entity.getLocationPosition(), entity.getLocationFloor(), entity.getLocationRoom(),
                       entity.getLocationIbm(), String.valueOf(entity.isStaus()));
               infoList.add(info);
           }
       }
       //设置表格样式
      table.getConfig().setContentCellBackgroundFormat(new BaseCellBackgroundFormat<CellInfo>() {
          @Override
          public int getBackGroundColor(CellInfo cellInfo) {
              return 0;
          }
      });
       table.getConfig().setContentStyle(new FontStyle(20,R.color.skyblue));
       //设置伸缩
       table.setZoom(true);
       //加载数据
       table.setData(infoList);
       if (infoList.size()==0){
           Toast.makeText(LogActivity.this,"暂无盘点日志3秒后自动退出",Toast.LENGTH_LONG).show();
           new RxTimer().timer(3, new RxTimer.IRxNext() {
               @Override
               public void doNext(long number, Disposable disposable) {
                   LogActivity.this.finish();
               }
           });
       }
    }

//    /**
//     * 绑定viewpager
//     */
//    private void initviewpager(){
//        MyFragmentPagerAdapter myFragmentPagerAdapter  =new MyFragmentPagerAdapter(getSupportFragmentManager());
//        myFragmentPagerAdapter.addFragment(new CensusFragment());
//        mViewPager.setAdapter(myFragmentPagerAdapter);
//        mTabLayout.setupWithViewPager(mViewPager);
//        mTabLayout.getTabAt(0).setText("盘点统计图");
//    }

    /**控件监听
     * @param v
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_back:
                this.finish();
                break;
            default:
                break;
        }
    }

    /**监听home键状态
     * @param keyCode
     * @param event
     * @return
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                this.finish();
                break;
            case KeyEvent.KEYCODE_MENU:
                this.finish();
                break;
            case KeyEvent.KEYCODE_HOME:
                this.finish();
                break;
            case KeyEvent.KEYCODE_APP_SWITCH:
                this.finish();
                break;
            default:
                break;
        }
        return super.onKeyDown(keyCode, event);
    }
}